package cn.iocoder.yudao.module.shop.staff.controller.admin;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.validation.groups.CreateGroup;
import cn.iocoder.yudao.framework.validation.groups.UpdateGroup;
import cn.iocoder.yudao.module.api.shop.info.ShopInfoApi;
import cn.iocoder.yudao.module.api.shop.info.dto.ShopInfoDTO;
import cn.iocoder.yudao.module.api.shop.staff.dto.ShopStaffSaveDTO;
import cn.iocoder.yudao.module.api.shop.staff.vo.ShopStaffVO;
import cn.iocoder.yudao.module.shop.staff.pojo.dto.ShopStaffPageDTO;
import cn.iocoder.yudao.module.shop.staff.pojo.po.ShopStaff;
import cn.iocoder.yudao.module.shop.staff.service.ShopStaffService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 核销人员")
@RestController
@RequestMapping({"/trade/verify-staff", "/shop/staff"})
@Validated
public class ShopStaffController {

    @Resource
    private ShopStaffService verifyStaffService;

    @Resource
    private ShopInfoApi shopInfoApi;

    @PostMapping("/create")
    @Operation(summary = "创建店员")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Long> createStaff(@Validated(value = {CreateGroup.class}) @RequestBody ShopStaffSaveDTO dto) {
        return success(verifyStaffService.createStaff(dto));
    }

    @RequestMapping(value = "/update", method = {RequestMethod.POST, RequestMethod.PUT})
    @Operation(summary = "更新店员")
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> updateStaff(@Validated(value = {UpdateGroup.class}) @RequestBody ShopStaffSaveDTO dto) {
        verifyStaffService.updateStaff(dto);
        return success(true);
    }

    @RequestMapping(value = "/delete", method = {RequestMethod.GET, RequestMethod.DELETE})
    @Operation(summary = "删除店员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasRole('shop_admin')")
    public CommonResult<Boolean> deleteStaff(@RequestParam("id") Long id) {
        verifyStaffService.deleteStaff(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得店员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('trade:verify-staff:query')")
    public CommonResult<ShopStaffVO> getStaff(@RequestParam("id") Long id) {
        ShopStaff verifyStaff = verifyStaffService.getStaff(id);
        return success(BeanUtils.toBean(verifyStaff, ShopStaffVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得店员分页")
    @PreAuthorize("@ss.hasPermission('trade:verify-staff:query')")
    public CommonResult<PageResult<ShopStaffVO>> getStaffPage(@Valid ShopStaffPageDTO dto) {
        PageResult<ShopStaff> pageResult = verifyStaffService.getStaffPage(dto);
        PageResult<ShopStaffVO> data = BeanUtils.toBean(pageResult, ShopStaffVO.class);
        Set<Long> shopIds = convertSet(data.getList(), ShopStaffVO::getShopId);
        Map<Long, ShopInfoDTO> shopMap = shopInfoApi.getShopInfoMap(shopIds);
        data.getList().forEach(vo -> vo.setShop(shopMap.get(vo.getShopId())));
        return success(data);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出店员 Excel")
    @PreAuthorize("@ss.hasPermission('trade:verify-staff:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportStaffExcel(@Valid ShopStaffPageDTO pageReqVO,
                                       HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ShopStaffVO> list = getStaffPage(pageReqVO).getData().getList();
        // 导出 Excel
        ExcelUtils.write(response, "店员.xls", "数据", ShopStaffVO.class, list);
    }

}